package com.ruitu.mall_98_seller.ui.msg;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.dao.bean.Msg;
import com.ruitu.mall_98_seller.dao.bean.Order;
import com.ruitu.mall_98_seller.mvp.model.MsgModel;
import com.ruitu.mall_98_seller.mvp.presenter.MsgPresenter;
import com.ruitu.mall_98_seller.ui.book.BookListActivity;
import com.ruitu.mall_98_seller.ui.order.OrderDetail2Activity;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.EventModel;

import java.text.SimpleDateFormat;

// 系统消息详情页面
public class MsgDetailActivity extends BaseActivity<MsgPresenter.MsgDedetialP, MsgModel.MsgDetailM> {
    private TextView tv_content, tv_theme, tv_time, tv_check;//消息内容,主题,时间,查看详情
    private LinearLayout ll_check;//查看详情

    private Msg msg = new Msg();

    protected int getLayoutRes() {
        return R.layout.activity_msg_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_content = findViewById(R.id.tv_content);
        tv_theme = findViewById(R.id.tv_theme);
        tv_time = findViewById(R.id.tv_time);
        tv_check = findViewById(R.id.tv_check);
        ll_check = findViewById(R.id.ll_check);

        setHeadTitle("消息详情");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String msgId = bundle.getString("msg_id");
            p.reqMsgDetail(msgId);
            showProgress();
        }
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 消息详情
            Arad.bus.post(new EventModel(18122851));
            msg = (Msg) data;
            if (null != msg) {
                setDataToView();
            } else {
                ToastUtils.showShortSafe("消息有误,请联系客服");
            }
        }
    }

    private void setDataToView() {
        tv_theme.setText(msg.getTitle());
        tv_content.setText(msg.getContent());
        try {
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.parseLong(msg.getCreateTime()));
            tv_time.setText(time);
        } catch (Exception e) {
            e.printStackTrace();
            tv_time.setText("1970-01-01 08:00:00");
        }

        Logg.i("orderId = " + msg.getOrderId() + "  msgType = " + msg.getMsgType());
        if (!TextUtils.isEmpty(msg.getOrderId())) {// 消息里面包含订单id,点击查看,跳转到订单详情
            ll_check.setVisibility(View.VISIBLE);
            ll_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Order o = new Order();
                    o.setId(msg.getOrderId());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("order", o);
                    startActivity(OrderDetail2Activity.class, bundle);
                }
            });
        } else {
            if (TextUtils.equals("10", msg.getMsgType())) {// 预订单 待审批
                ll_check.setVisibility(View.VISIBLE);
                tv_check.setText("预订审批");
                ll_check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (CommonUtils.isLoginToActivity(MsgDetailActivity.this)) {
                            startActivity(BookListActivity.class);
                        } else {
                            ToastUtils.showShortSafe("请先登录");
                        }
                    }
                });
            } else if (TextUtils.equals("12", msg.getMsgType())) {// 预订单 已拒绝
                ll_check.setVisibility(View.VISIBLE);
                tv_check.setText("预订审批");
                ll_check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (CommonUtils.isLoginToActivity(MsgDetailActivity.this)) {
                            startActivity(BookListActivity.class);
                        } else {
                            ToastUtils.showShortSafe("请先登录");
                        }
                    }
                });
            } else {
                ll_check.setVisibility(View.GONE);
            }
        }
    }
}
